27/01/26
API = Application Programming Interface
Goal: Format Request in specific way to get specific data from the server
an API for statistical agencies
each call must have identifiers for:
Statistical Agency (Provider)
Database (Dataflow)
Columns / Groupings (Dimensions), e.g.
A Basic call: chain all of these together to get the wanted dataset
very abstract, so lets see some
Goal: CPI data since 1960 for USA, United Kingdom, and Germany
| Dimension | Name | ID |
|---|---|---|
| Country | United Stations, United Kingdom, Germany | USA, GBR, DEU |
| Index Type | Consumer Price Index | CPI |
| Expenditure | All Items | _T |
| Type of Transformation | Period average, Y-O-Y percentage change | YOY_PCH_PA_PT |
| Frequency | Annual | A |
First, lets import the relevant libraries:
define our key by pasting together the selected IDs for each dimension
COUNTRIES <- "USA+GBR+DEU" # All countries
INDICATOR <- "CPI" # selected Indicator
EXPENDITURE <- "_T" # All items / Total
TRANSFORMATION <- "YOY_PCH_PA_PT" # Percentage change
FREQUENCY <- "A" # Annual
key <- paste0(COUNTRIES, ".", INDICATOR,".", EXPENDITURE, ".", TRANSFORMATION, ".", FREQUENCY)
key[1] "USA+GBR+DEU.CPI._T.YOY_PCH_PA_PT.A"
Now, we call the API, specifying our provider, database, key, and time period
[rsdmx][INFO] Fetching 'https://api.imf.org/external/sdmx/2.1/data/CPI/USA+GBR+DEU.CPI._T.YOY_PCH_PA_PT.A/all/?startPeriod=1960'
The response is in SDMX format, so we have to convert it to a data frame first
Lets make a quick plot
df %>%
select(COUNTRY, TIME_PERIOD, OBS_VALUE) %>% # select relevant columns
mutate( # change types of columns
OBS_VALUE = as.numeric(OBS_VALUE),
TIME_PERIOD = as.numeric(TIME_PERIOD)
) %>%
ggplot(aes(x=TIME_PERIOD, y=OBS_VALUE, color=COUNTRY)) + # basic ggplot call
geom_line() + # present as line plot
labs(title="CPI since 1960") # add titleGoal: Official Development Assistance (ODA)
now get the data and convert it to a data frame
a quick plot
The RSDMX Package
Unemployment Rate for Spain and Italy since 2012
Links:
Search for “Unemployment Rate” on data-explorer.oecd.org
Misleading: namend “monthly Unemployment Rate”, but has annual frequency as well
This Table has everything we need: Link
Coding Assistant
Note:
New format in RStudio
All packages in the tidyverse in R have very good cheat sheets!
[rsdmx][INFO] Fetching 'https://api.imf.org/external/sdmx/2.1/data/CPI/*.CPI._T.YOY_PCH_PA_PT.A/all/?startPeriod=1960'
[rsdmx][INFO] Attempt to fetch DSD ref from dataflow description
[rsdmx][INFO] Fetching 'https://api.imf.org/external/sdmx/2.1/dataflow/all/CPI/latest/'
[rsdmx][INFO] Fetching 'https://api.imf.org/external/sdmx/2.1/datastructure/all/DSD_CPI/latest/?references=descendants'
[rsdmx][INFO] DSD fetched and associated to dataset!
you can also download complete tables without specifying keys (Beware, it takes a lot of time!)